package lium.lbs.lbsim.activity

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentTransaction
import android.support.v4.content.ContextCompat
import android.view.KeyEvent
import android.view.View
import cn.jiguang.api.JCoreInterface
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main_bottom.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.fragments.*

abstract class MainBaseActivity: BaseActivity(){
    override fun getLayoutId(): Int { return R.layout.activity_main }
    var KEY_INDEX="index"
    var fragmentIndex=0

    override fun initView(savedInstanceState: Bundle?) {
        addOnClickListeners(R.id.fl_bottom_msglist,R.id.fl_bottom_contacts,R.id.fl_bottom_found,R.id.fl_bottom_my)
        chooseRadioButton(0)
    }

    public fun changeMainTopBarBg(iswhite :Boolean){
        if (iswhite){
            main_root.setBackgroundColor(ContextCompat.getColor(this,R.color.white))
        }else{
            main_root.setBackgroundColor(ContextCompat.getColor(this,R.color.color_F9FCFE))
        }

    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when(v!!.id){
            R.id.fl_bottom_msglist -> {chooseRadioButton(0) }
            R.id.fl_bottom_contacts -> {chooseRadioButton(1) }
            R.id.fl_bottom_found -> {chooseRadioButton(2) }
            R.id.fl_bottom_my -> {chooseRadioButton(3) }
        }
    }

    fun chooseRadioButton(postion:Int){
        fragmentIndex = postion
        when(postion){
            0 ->{
                rb_bottom_msglist.isChecked = true
                rb_bottom_contacts.isChecked = false
                rb_bottom_found.isChecked = false
                rb_bottom_my.isChecked = false
                chooseFragment( ConversationListFragment())
                changeMainTopBarBg(false)
            }
            1 ->{
                rb_bottom_msglist.isChecked = false
                rb_bottom_contacts.isChecked = true
                rb_bottom_found.isChecked = false
                rb_bottom_my.isChecked = false
                chooseFragment(ContactsFragment.newInstance())
                changeMainTopBarBg(true)
            }
            2 ->{
                rb_bottom_msglist.isChecked = false
                rb_bottom_contacts.isChecked = false
                rb_bottom_found.isChecked = true
                rb_bottom_my.isChecked = false
                chooseFragment(FoundFragment.newInstance())
                changeMainTopBarBg(true)
            }
            3 ->{
                rb_bottom_msglist.isChecked = false
                rb_bottom_contacts.isChecked = false
                rb_bottom_found.isChecked = false
                rb_bottom_my.isChecked = true
                chooseFragment(MyFragment.newInstance())
                changeMainTopBarBg(true)
            }
        }
    }

    private fun chooseFragment(fragment: Fragment) {
        var fragmentTransaction: FragmentTransaction = supportFragmentManager
            .beginTransaction()
        fragmentTransaction.replace(R.id.main_panel,fragment,fragment.javaClass.name) .addToBackStack(null)
        fragmentTransaction.commit()
    }


    override fun getNetWorkData() {
    }
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        // 保存当前Fragment的下标
        outState.putInt(KEY_INDEX, fragmentIndex)
    }


    override fun onPause() {
        JCoreInterface.onPause(this)
        super.onPause()
    }

    override fun onResume() {
        JCoreInterface.onResume(this)
        super.onResume()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        return keyCode == KeyEvent.KEYCODE_BACK
    }

}